﻿using System;
using System.Xml.Linq;
using System.Net;
using System.ComponentModel;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TwitterClient
{
    public static partial class ParseExtends
    {
        //#region Public Properties

        //#region public DateTime CreatedAt { get; set; }
        //private DateTime _CreatedAt;
        //public DateTime CreatedAt
        //{
        //    get { return _CreatedAt; }
        //    set { _CreatedAt = value; OnPropertyChanged("CreatedAt"); }
        //}
        //#endregion

        //#region public long ID { get; set; }
        //private long _ID;
        //public long ID
        //{
        //    get { return _ID; }
        //    set { _ID = value; OnPropertyChanged("ID"); }
        //}
        //#endregion

        //#region public string Text { get; set; }
        //private string _Text;
        //public string Text
        //{
        //    get { return _Text; }
        //    set { _Text = value; OnPropertyChanged("Text"); }
        //}
        //#endregion

        //#region public string Source { get; set; }
        //private string _Source;
        //public string Source
        //{
        //    get { return _Source; }
        //    set { _Source = value; OnPropertyChanged("Source"); }
        //}
        //#endregion

        //#region public bool IsTruncated { get; set; }
        //private bool _IsTruncated;
        //public bool IsTruncated
        //{
        //    get { return _IsTruncated; }
        //    set { _IsTruncated = value; OnPropertyChanged("IsTruncated"); }
        //}
        //#endregion

        //#region public User User { get; set; }
        //private User _User;
        //public User User
        //{
        //    get { return _User; }
        //    set { _User = value; OnPropertyChanged("User"); }
        //}
        //#endregion

        //#region public long InReplyToStatusID { get; set; }
        //private long _InReplyToStatusID;
        //public long InReplyToStatusID
        //{
        //    get { return _InReplyToStatusID; }
        //    set { _InReplyToStatusID = value; OnPropertyChanged("InReplyToStatusID"); }
        //}
        //#endregion

        //#region public long InReplyToUserID { get; set; }
        //private long _InReplyToUserID;
        //public long InReplyToUserID
        //{
        //    get { return _InReplyToUserID; }
        //    set { _InReplyToUserID = value; OnPropertyChanged("InReplyToUserID"); }
        //}
        //#endregion

        //#region public bool IsFavorited { get; set; }
        //private bool _IsFavorited;
        //public bool IsFavorited
        //{
        //    get { return _IsFavorited; }
        //    set { _IsFavorited = value; OnPropertyChanged("IsFavorited"); }
        //}
        //#endregion

        //#region public string InReplyToScreenName { get; set; }
        //private string _InReplyToScreenName;
        //public string InReplyToScreenName
        //{
        //    get { return _InReplyToScreenName; }
        //    set { _InReplyToScreenName = value; OnPropertyChanged("InReplyToScreenName"); }
        //}
        //#endregion

        //#endregion

        #region Public Static Methods

        public static Status Parse(this Status status, string xml, ITwitterCommand cmd)
        {
            XElement element = XElement.Parse(xml);
            //Status status = new Status();
            status.CreatedAt = DateTime.ParseExact(element.Element("created_at").Value, "ddd MMM d HH':'mm':'ss zzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);//
            status.StatusID = Convert.ToInt64(element.Element("id").Value);
            status.Text = WebUtility.HtmlDecode(element.Element("text").Value);
            status.Source = WebUtility.HtmlDecode(element.Element("source").Value);
            status.IsTruncated = bool.Parse(element.Element("truncated").Value);

            string in_reply_to_user_id = element.Element("in_reply_to_user_id").Value;
            if (!string.IsNullOrEmpty(in_reply_to_user_id) && in_reply_to_user_id != "0")
            {
                status.InReplyToUserID = Convert.ToInt64(in_reply_to_user_id);
            }
            else
            {
                status.InReplyToUserID = null;
            }

            string in_reply_to_status_id = element.Element("in_reply_to_status_id").Value;
            if (!string.IsNullOrEmpty(in_reply_to_status_id) && in_reply_to_status_id != "0")
            {
                status.InReplyToStatusID = Convert.ToInt64(in_reply_to_status_id);
            }
            else
            {
                status.InReplyToStatusID = null;
            }

            status.IsFavorited = bool.Parse(element.Element("favorited").Value);
            status.InReplyToScreenName = WebUtility.HtmlDecode(element.Element("in_reply_to_screen_name").Value);
            status.UserID = long.Parse(element.Element("user").Element("id").Value);

            var userElement = element.Element("user");
            if (userElement != null)
                status.User = new User().Parse(userElement.ToString());
            else
                status.User = null;

            //InReplyToUserはTwitterは返さないので別途取得する
            //if (cmd != null && status.InReplyToUserID != null)
            //{
            //    status.InReplyToUser = cmd.ShowUser(status.InReplyToUserID.Value);
            //}

            return status;
        }

        #endregion

    }
}
